package lee.vioson.ebook.ui.components

import android.app.Activity
import android.content.Intent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.laiyifen.lyftoast.ToastUtils
import lee.vioson.composecommon.base.mvi.BaseViewModel

/**
 *Author:viosonlee
 *Date:2023/5/25
 *DESCRIPTION:
 */
@Composable
fun MviScreen(
    viewModel: BaseViewModel<*, *>,
    content: @Composable () -> Unit
) {

    val context = LocalContext.current
    var showLoading by rememberSaveable { mutableStateOf(false) }
    val loading by viewModel.loading.observeAsState()

    var error by rememberSaveable { mutableStateOf<Throwable?>(null) }
    val throwable by viewModel.error.observeAsState()

    LaunchedEffect(key1 = throwable, block = {
        if(error != throwable){
            error = throwable
        }
    })

    LaunchedEffect(key1 = error, block = {
        error?.let {
            ToastUtils.showError(context, it.localizedMessage)
            showLoading = false
        }
    })

    LaunchedEffect(key1 = loading, block = {
        loading?.let {
            showLoading = it.show
        }
    })

    val tokenError by viewModel.tokenError.observeAsState()
    LaunchedEffect(key1 = tokenError, block = {
        tokenError?.let {
            ToastUtils.showError(context, it)
            showLoading = false
            //to login

        }
    })
    val navigationAction by viewModel.navigationAction.observeAsState()
    LaunchedEffect(key1 = navigationAction, block = {
        navigationAction?.let {
            if (it.requestCode == null) {
                context.startActivity(Intent(context, it.classT).apply(it.intentBlock ?: {}))
            } else {
                (context as? Activity)?.startActivityForResult(
                    Intent(context, it.classT).apply(it.intentBlock ?: {}),
                    it.requestCode!!
                )
            }
            if (it.withFinish) {
                (context as? Activity)?.finish()
            }
        }
    })
    Box(modifier = Modifier.fillMaxSize()) {
        CompositionLocalProvider(content = content)
        if (showLoading) {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                CircularProgressIndicator(modifier = Modifier.size(60.dp))
            }
        }
    }
}